import BaseMonsterSkill from "./BaseMonsterSkill";
import IBaseMonsterSkill from "./IBaseMonsterSkill";
import MonsterScript from "../../../component/MonsterScript";
import SkillEffect from "./SkillEffect";
import Game from "../../Game";





export default class ShieldSkill extends BaseMonsterSkill implements IBaseMonsterSkill {


    _shield: Laya.ShuriKenParticle3D;
    _explosion: Laya.ShuriKenParticle3D;

    _scale: number = 1;

    _times: number = 1;

    _curTimes: number = 0;

    _scale2: number = 0.05;

    init(target: MonsterScript, skillId) {
        super.init(target, skillId);
        let effect: Laya.ShuriKenParticle3D = SkillEffect.getShieldEffect(this._monsterSkillVO.resPath)
        this._shield = effect.getChildByName("skill_shield_ring") as Laya.ShuriKenParticle3D;
        this._explosion = effect.getChildByName("sparks") as Laya.ShuriKenParticle3D;
        this._scale = this._skillData&&this._skillData.scale ? this._skillData.scale : this._scale;
        this._scale2 = this._skillData&&this._skillData.scale2 ? this._skillData.scale2 : this._scale2;
        this._times = this._skillData&&this._skillData.times ? this._skillData.times : this._times;
        this._shield.transform.localScaleX = this._scale;
        this._shield.transform.localScaleY = this._scale;
        this._shield.transform.localScaleZ = this._scale;

        this._explosion.transform.localScaleX = this._scale2;
        this._explosion.transform.localScaleY = this._scale2;
        this._explosion.transform.localScaleZ = this._scale2;

        // Game.layer3d.addChild(this._explosion);
        this._target.sp3d.addChild(this._explosion);
        this._target.setShowHitEffect(false);
        this._target.addHitCb(this.hitCb.bind(this))
    }

    hitCb() {
        if (this._curTimes >= this._times) {
            this._target.setShowHitEffect(true);
            return false;
        }
        else {
            this._explosion.transform.position = this._target.sp3d.transform.position;
            this._explosion.particleSystem.play()
            this._curTimes++;
            if (this._curTimes >= this._times) {
                this._target.sp3d.removeChild(this._shield);
            }
            return true;
        }

    }


    exec() {
        // console.log("ShieldSkill exec")
        if (this._isSkilling === false) {
            this._isSkilling = true;
            if (!this._target.sp3d.getChildByName("skill_shield_ring")) {
                this._target.sp3d.addChild(this._shield);
            }
        }

    }

    reset() {
        this._target.setShowHitEffect(false);
        super.reset();
        if(!this._target.sp3d.getChildByName("skill_shield_ring")){
            this._target.sp3d.addChild(this._shield);
        }
        this._curTimes = 0;
   
    }

    destroy() {
        this._target.setShowHitEffect(true);
        Game.layer3d.removeChild(this._explosion);
        this._target.sp3d.removeChild(this._shield);
        // this._target.sp3d.removeChild(this._explosion);
        this._shield = null;
        this._explosion = null;
        super.destroy();
    }





}